<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/13
 * Time: 10:00
 */

namespace App\Http\Controllers\Project;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\AdminModel;
use App\Http\Model\GroupMembersModel;
use App\Http\Model\GroupModel;
use App\Http\Model\HandymanAccountsModel;
use App\Http\Model\ProfessionModel;
use App\Http\Model\ProjectAreaModel;
use App\Http\Model\ProjectBudgetModel;
use App\Http\Model\ProjectModel;
use App\Http\Model\ProjectSectionModel;
use App\Http\Model\WorkmanAccountsModel;
use App\Http\Model\WorkmanAssignmentModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ProjectController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function selectLists(Request $request)
    {
        $rules = [
            'isAll' => 'nullable|integer|in:0,1',
        ];
        $message = [
            'isAll.integer' => '参数类型错误',
            'isAll.in' => '参数值不正确',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $this->data = $projectModel->selectLists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 320101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $lists = $projectModel->lists($param);
            $countLists = $projectModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 320101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'name' => 'required',
            'city' => 'required',
//            'projectAmount' => 'nullable|numeric',
//            'projectAccount' => 'nullable|numeric',
            'order' => 'integer',
        ];
        $message = [
            'name.required' => '请填写项目名称',
            'city.required' => '请填写城市',
//            'projectAmount.numeric' => '项目工程量类型错误',
//            'projectAccount.numeric' => '项目工程款类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['name', 'city', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $result = $projectModel->insert($param);
            if (!$result) {
                throw new ApiException('保存失败，请稍后重试', 320202);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 320201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取项目参数失败',
            'id.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $this->data = $projectModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 320301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'city' => 'required',
            'projectAmount' => 'nullable|numeric',
            'projectAccount' => 'nullable|numeric',
            'order' => 'integer',
        ];
        $message = [
            'id.required' => '获取项目参数失败',
            'id.integer' => '项目参数类型错误',
            'name.required' => '请填写项目名称',
            'city.required' => '请填写城市',
            'projectAmount.numeric' => '项目工程量类型错误',
            'projectAccount.numeric' => '项目工程款类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['id', 'name', 'city', 'projectAmount', 'projectAccount', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $info = $projectModel->info(['id' => $param['id']]);
            if ($info['status'] == 2) {
                throw new ApiException('该项目已完工，项目信息不能修改', 320402);
            }
            if (empty($param['order'])) {
                $order = 1;
            } else {
                $order = $param['order'];
            }
            $data = [
                'name' => $param['name'],
                'city' => $param['city'],
//                'projectAmount'=>$param['projectAmount'],
//                'projectAccount'=>$param['projectAccount'],
                'order' => $order,
            ];
            $projectModel->update(['id' => $param['id']], $data);
        } else {
            throw new ApiException($validator->errors()->first(), 320401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取项目参数失败',
            'id.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['id', 'status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $projectModel->update(['id' => $param['id']], ['status' => 2]);
        } else {
            throw new ApiException($validator->errors()->first(), 320501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function areaLists(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
            'professionId' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'length.required' => '获取每页记录数参数失败',
            'length.integer' => '每页记录数参数类型错误',
            'length.in' => '每页记录数参数值不正确',
            'page.required' => '获取起始记录参数失败',
            'page.integer' => '起始记录参数类型错误',
            'page.min' => '起始记录参数值不小于:min',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['projectId', 'search', 'length', 'page', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $lists = $projectAreaModel->lists($param);
            $countLists = $projectAreaModel->countLists($param);
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $handymanAccountModel = new HandymanAccountsModel();
            $projectSectionModel = new ProjectSectionModel();
            $projectBudgetModel = new ProjectBudgetModel();
            $professionModel = new ProfessionModel();
            $formwork = 0;
            if (!empty($param['professionId'])) {
                $profession = $professionModel->info(['id' => $param['professionId']]);
                $formwork = $profession['isFormwork'] === 1 ? 1 : 0;
            }

            foreach ($lists as $key => $l) {
                $param['areaId'] = $l->id;
                $lists[$key]->workmanAssignment = $workmanAssignmentModel->getSum($param);
                $lists[$key]->handymanAccounts = $handymanAccountModel->getSum($param);
                $areaLists = $projectSectionModel->sumArea($l->id);
                $area = $lists[$key]->area = $areaLists['area'];
                $formworkArea = $lists[$key]->formworkArea = $areaLists['formworkArea'];
                $budgetFormworkArea = $lists[$key]->budgetFormworkArea = $areaLists['budgetFormworkArea'];
                $budget = $projectBudgetModel->getSum($param);
                $budgetArea = $formwork === 1 ? (float)$budgetFormworkArea : $area;
                $budgetPrice = $budgetArea == 0 ? 0 : $budget / $budgetArea;
                $area = $formwork === 1 ? $formworkArea : $area;
                $price = $area == 0 ? 0 : ($lists[$key]->workmanAssignment + $lists[$key]->handymanAccounts) / $area;
                if ($budgetPrice < $price && $budgetPrice > 0) {
                    $lists[$key]->budgetPriceAlert = 1;
                } else {
                    $lists[$key]->budgetPriceAlert = 0;
                }
            }
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 320601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addArea(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'name' => 'required',
            'order' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'name.required' => '请填写项目名称',
            'order.integer' => '请填写排序',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['projectId', 'name', 'remark', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $info = $projectAreaModel->checkRepeat(['projectId' => $param['projectId'], 'name' => $param['name']]);
            if (!empty($info)) {
                throw new ApiException('已存在相同名称的施工区', 320702);
            }
            $projectAreaModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 320701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function batchAddArea(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'amount' => 'required|integer|min:1',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'amount.required' => '请填写批量增加数量',
            'amount.integer' => '批量增加数量数据类型错误',
            'amount.min' => '批量增加数量不能小于:min',
        ];
        $param = $request->only(['projectId', 'amount']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $batchInfo = $projectAreaModel->batchInfo($param);
            $maxOrder = $projectAreaModel->getMaxOrder(['projectId' => $param['projectId']]);
            if (empty($batchInfo)) {
                $start = 1;
            } else {
                $start = intval(substr($batchInfo['name'], 0, -2)) + 1;
            }
            for ($i = $start; $i < $start + $param['amount']; $i++) {
                $projectAreaModel->insert(['projectId' => $param['projectId'], 'name' => $i . '号楼', 'order' => ($maxOrder + $i)]);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 320801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function areaInfo(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取施工区参数失败',
            'id.integer' => '施工区参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $this->data = $projectAreaModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 320901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editArea(Request $request)
    {
        $rules = [
            'id' => 'required',
            'name' => 'required',
            'order' => 'integer',
        ];
        $message = [
            'id.required' => '获取施工区参数失败',
            'name.required' => '请填写项目名称',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['id', 'name', 'remark', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $info = $projectAreaModel->info(['id' => $param['id']]);
            $info = $projectAreaModel->checkRepeat(['projectId' => $info['projectId'], 'name' => $param['name']], $param['id']);
            if (!empty($info)) {
                throw new ApiException('已存在相同名称的施工区', 321002);
            }
            $id = $param['id'];
            unset($param['id']);
            if (!isset($param['order']) || empty($param['order'])) {
                $param['order'] = 1;
            }
            $projectAreaModel->update(['id' => $id], $param);
        } else {
            throw new ApiException($validator->errors()->first(), 321001);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delArea(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取施工区参数失败',
            'id.integer' => '施工区参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $result = $projectSectionModel->lists(['areaId' => $param['id']]);
            if (!empty($result)) {
                throw new ApiException('该楼栋下已建立楼层，不能删除', 321102);
            }
            $projectAreaModel = new ProjectAreaModel();
            $projectAreaModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 321101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function areaSelectLists(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['projectId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $this->data = $projectAreaModel->selectLists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 321201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function sectionLists(Request $request)
    {
        $rules = [
            'areaId' => 'required|integer',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
            'professionId' => 'nullable|integer',
        ];
        $message = [
            'areaId.required' => '获取施工区参数失败',
            'areaId.integer' => '施工区参数类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '起始记录参数类型错误',
            'page.min' => '起始记录值不小于:min',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['areaId', 'search', 'length', 'page', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $lists = $projectSectionModel->lists($param);
            $countLists = $projectSectionModel->countLists($param);
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $handymanAccountModel = new HandymanAccountsModel();
            $projectBudgetModel = new ProjectBudgetModel();
            $professionModel = new ProfessionModel();
            $formwork = 0;
            if (!empty($param['professionId'])) {
                $profession = $professionModel->info(['id' => $param['professionId']]);
                $formwork = $profession['isFormwork'] === 1 ? 1 : 0;
            }

            foreach ($lists as $key => $l) {
                $param['sectionId'] = $l->id;
                $lists[$key]->workmanAssignment = $workmanAssignmentModel->getSum($param);
                $lists[$key]->handymanAccounts = $handymanAccountModel->getSum($param);
                $budget = $projectBudgetModel->getSum($param);
                $budgetArea = $formwork === 1 ? (float)$l->budgetFormworkArea : $l->area;
                $budgetPrice = $budgetArea == 0 ? 0 : $budget / $budgetArea;
                $area = $formwork === 1 ? $l->formworkArea : $l->area;
                $price = $area == 0 ? 0 : ($lists[$key]->workmanAssignment + $lists[$key]->handymanAccounts) / $area;
                if ($budgetPrice < $price && $budgetPrice > 0) {
                    $lists[$key]->budgetPriceAlert = 1;
                } else {
                    $lists[$key]->budgetPriceAlert = 0;
                }
            }
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 321301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addSection(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'areaId' => 'required|integer',
            'area' => 'nullable|numeric',
            'name' => 'required',
            'order' => 'integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'areaId.required' => '获取施工段参数失败',
            'areaId.integer' => '施工段参数类型错误',
            'area.numeric' => '施工段面积参数类型错误',
            'formworkArea.numeric' => '施工段模板面积参数类型错误',
            'name.required' => '请填写项目名称',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['projectId', 'areaId', 'name', 'area', 'remark', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $info = $projectSectionModel->checkRepeat(['projectId' => $param['projectId'], 'areaId' => $param['areaId'], 'name' => $param['name']]);
            if (!empty($info)) {
                throw new ApiException('已存在相同名称的施工段', 321402);
            }
            $projectSectionModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 321401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function batchAddSection(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'areaId' => 'required|integer',
            'amount' => 'required|integer|min:1',
            'area' => 'nullable|numeric',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'areaId.required' => '获取施工区参数失败',
            'areaId.integer' => '施工区参数类型错误',
            'amount.required' => '请填写批量增加数量',
            'amount.integer' => '批量增加数量数据类型错误',
            'amount.min' => '批量增加数量不能小于:min',
            'area.numeric' => '施工区面积数据类型错误',
        ];
        $param = $request->only(['projectId', 'areaId', 'amount', 'area']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $batchInfo = $projectSectionModel->batchInfo($param);
            $maxOrder = $projectSectionModel->getMaxOrder(['areaId' => $param['areaId']]);
            if (empty($batchInfo)) {
                $start = 1;
            } else {
                $start = intval(substr($batchInfo['name'], 0, -2)) + 1;
            }
            for ($i = $start; $i < $start + $param['amount']; $i++) {
                $projectSectionModel->insert(['projectId' => $param['projectId'], 'areaId' => $param['areaId'], 'name' => $i . '层', 'area' => $param['area'], 'order' => ($maxOrder + $i)]);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 321501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function sectionInfo(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取施工段参数失败',
            'id.integer' => '施工段参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $this->data = $projectSectionModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 321601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editSection(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'area' => 'nullable|numeric',
            'order' => 'integer',
        ];
        $message = [
            'id.required' => '获取施工区参数失败',
            'id.integer' => '施工区参数类型错误',
            'name.required' => '请填写项目名称',
            'area.numeric' => '施工段面积参数类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['id', 'name', 'area', 'remark', 'order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $info = $projectSectionModel->info(['id' => $param['id']]);
            $repeat = $projectSectionModel->checkRepeat(['projectId' => $info['projectId'], 'areaId' => $info['areaId'], 'name' => $param['name']], $param['id']);
            if (!empty($repeat)) {
                throw new ApiException('已存在相同名称的施工段', 321702);
            }
            $id = $param['id'];
            unset($param['id']);
            if (!isset($param['order']) || empty($param['order'])) {
                $param['order'] = 1;
            }
            $projectSectionModel->update(['id' => $id], $param);
//            $projectAreaModel = new ProjectAreaModel();
//            $projectAreaModel->updateAreaArea($info['areaId']);
        } else {
            throw new ApiException($validator->errors()->first(), 321701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delSection(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取施工区参数失败',
            'id.integer' => '施工区参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $projectBudgetModel = new ProjectBudgetModel();
            $result = $workmanAssignmentModel->info(['sectionId' => $param['id']]);
            $result1 = $projectBudgetModel->info(['sectionId' => $param['id']]);
            if (!empty($result) || !empty($result1)) {
                throw new ApiException('该楼层下已建立记录分账或预算信息，不能删除', 321802);
            }
            $projectSectionModel = new ProjectSectionModel();
            $info = $projectSectionModel->info($param);
            $projectSectionModel->delete($param);

//            $projectAreaModel = new ProjectAreaModel();
//            $projectAreaModel->updateAreaArea($info['areaId']);
        } else {
            throw new ApiException($validator->errors()->first(), 321801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function sectionSelectLists(Request $request)
    {
        $rules = [
            'areaId' => 'required|integer',
            'approval' => 'nullable|integer|in:1'
        ];
        $message = [
            'areaId.required' => '获取施工区参数失败',
            'areaId.integer' => '施工区参数类型错误',
        ];
        $param = $request->only(['areaId', 'approval']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $this->data = $projectSectionModel->selectLists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 321901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function setFormworkArea(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'formworkArea' => 'nullable|numeric'
        ];
        $message = [
            'id.required' => '获取施工段参数失败',
            'id.integer' => '施工段参数类型错误',
            'formworkArea.numeric' => '模板面积参数类型错误',
        ];
        $param = $request->only(['id', 'formworkArea']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $projectSectionModel->update(['id' => $param['id']], ['formworkArea' => $param['formworkArea']]);
        } else {
            throw new ApiException($validator->errors()->first(), 321901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function setBudgetFormworkArea(Request $request){
        $rules = [
            'id' => 'required|integer',
            'budgetFormworkArea' => 'nullable|numeric'
        ];
        $message = [
            'id.required' => '获取施工段参数失败',
            'id.integer' => '施工段参数类型错误',
            'budgetFormworkArea.numeric' => '模板面积参数类型错误',
        ];
        $param = $request->only(['id', 'budgetFormworkArea']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $projectSectionModel->update(['id' => $param['id']], ['budgetFormworkArea' => $param['budgetFormworkArea']]);
        } else {
            throw new ApiException($validator->errors()->first(), 321901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function costArea(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'areaId' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'areaId.integer' => '施工区参数类型错误',
        ];
        $param = $request->only(['projectId', 'areaId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $handymanAccountsModel = new HandymanAccountsModel();
            $professionModel = new ProfessionModel();
            $this->data = $professionModel->selectLists();
            foreach ($this->data as $k => $l) {
                $this->data[$k]->workmanAssignment = $workmanAssignmentModel->getSum(['areaId' => $param['areaId'], 'professionId' => $l->id]);
                $this->data[$k]->handymanAccounts = $handymanAccountsModel->getSum(['areaId' => $param['areaId'], 'professionId' => $l->id]);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function costSection(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.integer' => '施工段参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $handymanAccountsModel = new HandymanAccountsModel();
            $professionModel = new ProfessionModel();
            $this->data = $professionModel->selectLists();
            foreach ($this->data as $k => $l) {
                $this->data[$k]->workmanAssignment = $workmanAssignmentModel->getSum(['sectionId' => $param['sectionId'], 'professionId' => $l->id]);
                $this->data[$k]->handymanAccounts = $handymanAccountsModel->getSum(['sectionId' => $param['sectionId'], 'professionId' => $l->id]);

            }
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function budgetArea(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '起始记录参数类型错误',
            'page.min' => '起始记录值不小于:min',
        ];
        $param = $request->only(['projectId', 'search', 'length', 'page']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectAreaModel = new ProjectAreaModel();
            $projectSectionModel = new ProjectSectionModel();
            $projectBudgetModel = new ProjectBudgetModel();
            $professionModel = new ProfessionModel();
            $workmanAccountsModel = new WorkmanAccountsModel();
            $handymanAccountsModel = new HandymanAccountsModel();
            $lists = $projectAreaModel->lists($param);
            $countLists = $projectAreaModel->countLists($param);
            $professionLists = $professionModel->selectLists();
            foreach ($lists as $k => $l) {
                $lists[$k]->budget = $projectBudgetModel->getSum(['areaId' => $l->id]);
                $areaLists = $projectSectionModel->sumArea($l->id);
                $lists[$k]->area = $areaLists['area'];
                $lists[$k]->formworkArea = $areaLists['formworkArea'];
                $lists[$k]->budgetFormworkArea = $areaLists['budgetFormworkArea'];
                $lists[$k]->cost = $workmanAccountsModel->getSum(['areaId' => $l->id])+$handymanAccountsModel->getSum(['areaId' => $l->id]);
                $lists[$k]->budgetPrice =$lists[$k]->area==0?'-':$lists[$k]->budget / $lists[$k]->area;
                $lists[$k]->costPrice =$lists[$k]->area==0?'-':$lists[$k]->cost / $lists[$k]->area;
                foreach ($professionLists as $p) {
                    // 预算
                    $v1 = 'b_p_' . $p->id;
                    $lists[$k]->$v1 = $projectBudgetModel->getSum(['areaId' => $l->id, 'professionId' => $p->id]);
                    $v2 = 'b_pp_' . $p->id;
                    $area1 = $p->isFormwork == 1 ? $lists[$k]->budgetFormworkArea : $lists[$k]->area;
                    if ($area1 == 0) {
                        $lists[$k]->$v2 = '-';
                    } else {
                        $lists[$k]->$v2 = $lists[$k]->$v1 / $area1;
                    }
                    // 成本
                    $v3 = 'p_' . $p->id;
                    $wa = $workmanAccountsModel->getSum(['areaId' => $l->id, 'professionId' => $p->id]);
                    $ha = $handymanAccountsModel->getSum(['areaId' => $l->id, 'professionId' => $p->id]);
                    $lists[$k]->$v3 = $wa + $ha;
                    // 成本单价
                    $v4 = 'pp_' . $p->id;
                    $area2 = $p->isFormwork == 1 ? $lists[$k]->formworkArea : $lists[$k]->area;
                    if ($area2 == 0) {
                        $lists[$k]->$v4 = '-';
                    } else {
                        $lists[$k]->$v4 = $lists[$k]->$v3 / $area2;
                    }
                }
            }
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function budgetSection(Request $request)
    {
        $rules = [
            'areaId' => 'required|integer',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'areaId.required' => '获取项目参数失败',
            'areaId.integer' => '项目参数类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '起始记录参数类型错误',
            'page.min' => '起始记录值不小于:min',
        ];
        $param = $request->only(['areaId', 'search', 'length', 'page']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $projectBudgetModel = new ProjectBudgetModel();
            $professionModel = new ProfessionModel();
            $workmanAccountsModel = new WorkmanAccountsModel();
            $handymanAccountsModel = new HandymanAccountsModel();
            $lists = $projectSectionModel->lists($param);
            $countLists = $projectSectionModel->countLists($param);
            $professionLists = $professionModel->selectLists();
            foreach ($lists as $k => $l) {
                $lists[$k]->budget = $projectBudgetModel->getSum(['sectionId' => $l->id]);
                $lists[$k]->cost = $workmanAccountsModel->getSum(['sectionId' => $l->id])+$handymanAccountsModel->getSum(['areaId' => $l->id]);
                $lists[$k]->budgetPrice =$lists[$k]->area==0?'-':$lists[$k]->budget / $lists[$k]->area;
                $lists[$k]->costPrice =$lists[$k]->area==0?'-':$lists[$k]->cost / $lists[$k]->area;
                foreach ($professionLists as $p) {
                    $info = $projectBudgetModel->info(['sectionId' => $l->id, 'professionId' => $p->id]);
                    $v1 = 'b_p_' . $p->id;
                    $lists[$k]->$v1 = empty($info['totalPrice'])?0:$info['totalPrice'];
                    $v2 = 'b_pp_' . $p->id;
                    $lists[$k]->$v2 = empty($info['price'])?0:$info['price'];

                    $v3 = 'p_' . $p->id;
                    $wa = $workmanAccountsModel->getSum(['sectionId' => $l->id, 'professionId' => $p->id]);
                    $ha = $handymanAccountsModel->getSum(['sectionId' => $l->id, 'professionId' => $p->id]);
                    $lists[$k]->$v3 = $wa + $ha;
                    $v4 = 'pp_' . $p->id;
                    $area2 = $p->isFormwork == 1 ? $l->formworkArea : $l->area;
                    if ($area2 == 0) {
                        $lists[$k]->$v4 = '-';
                    } else {
                        $lists[$k]->$v4 = $lists[$k]->$v3 / $area2;
                    }
                }
            }
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function budgetDetail(Request $request)
    {
        $rules = [
            'sectionId' => 'required|integer',
        ];
        $message = [
            'sectionId.required' => '获取项目参数失败',
            'sectionId.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['sectionId', 'search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectSectionModel = new ProjectSectionModel();
            $projectBudgetModel = new ProjectBudgetModel();
            $professionModel = new ProfessionModel();
            $this->data = $professionModel->selectLists();
            $info = $projectSectionModel->info(['id'=>$param['sectionId']]);
            foreach ($this->data as $k => $d) {
                $budget = $projectBudgetModel->info(['sectionId' => $param['sectionId'], 'professionId' => $d->id]);
                $this->data[$k]->area = $info['area'];
                $this->data[$k]->formworkArea = $info['formworkArea'];
                $this->data[$k]->totalPrice =empty($budget['totalPrice'])?0:$budget['totalPrice'];
                $this->data[$k]->price =empty($budget['price'])?0:$budget['price'];
                $this->data[$k]->budgetId = empty($budget['id'])?'':$budget['id'];
            }

        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editBudget(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'areaId' => 'required|integer',
            'sectionId' => 'required|integer',
            'data' => 'required|array'
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'areaId.required' => '获取施工区参数失败',
            'areaId.integer' => '施工区参数类型错误',
            'sectionId.required' => '获取施工段参数失败',
            'sectionId.integer' => '施工段参数类型错误',
            'data.required' => '获取预算数据失败',
            'data.array' => '数据类型错误',
        ];
        $param = $request->only('projectId', 'areaId', 'sectionId', 'data');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rules1 = [
                'professionId' => 'required|integer',
                'price' => 'required|numeric',
//                'totalPrice' => 'required|numeric'
            ];
            $message1 = [
                'professionId.required' => '获取工种参数失败',
                'professionId.integer' => '工种参数类型错误',
                'price.required' => '获取单价参数失败',
                'price.numeric' => '单价参数类型错误',
//                'totalPrice.required' => '获取工种预算总价数据失败',
//                'totalPrice.numeric' => '工种预算总价类型错误',
            ];
            foreach ($param['data'] as $d) {
                $validator1 = Validator::make($d, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator->errors()->first(), 322101);
                }
            }
            $param['adminId'] = $request->session()->get('adminId');
            $projectBudgetModel = new ProjectBudgetModel();
            $projectBudgetModel->editBudget($param);
        } else {
            throw new ApiException($validator->errors()->first(), 322101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 项目下某施工段某工种，所有参与施工的班组的施工量价清单及人员分账
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function separateLog(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $this->data = $groupModel->lists(['projectId' => $param['projectId'], 'professionId' => $param['professionId']]);
            foreach ($this->data as $key => $d) {
                $separateInfo = $this->getSeparateLogInfo($param['projectId'], $param['sectionId'], $d->id);
                if (empty($separateInfo)) {
                    unset($this->data[$key]);
                } else {
                    $this->data[$key]->assignment = $separateInfo['assignment'];
                    $this->data[$key]->separate = $separateInfo['separate'];
                }
            }
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addAssignment(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'areaId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
            'groupId' => 'required|integer',
            'save' => 'required|integer|in:0,1',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'areaId.required' => '获取施工段参数失败',
            'areaId.integer' => '施工段参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'groupId.required' => '获取班组参数失败',
            'groupId.integer' => '班组参数类型错误',
            'data.required' => '获取施工量价数据失败',
            'data.array' => '施工量价参数类型错误',
            'save.required' => '获取临时参数失败',
            'save.integer' => '临时参数类型错误',
        ];
        $param = $request->only('projectId', 'areaId', 'sectionId', 'professionId', 'groupId', 'data', 'save');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $adminId = $request->session()->get('adminId');

            $rules1 = [
                'id' => 'nullable|integer',
                'assignmentId' => 'required|integer',
                'amount' => 'required|numeric',
                'price' => 'required|numeric',
                'totalPrice' => 'required|numeric',
            ];
            $message1 = [
                'id.integer' => '施工量记录参数类型错误',
                'assignmentId.required' => '获取施工项参数失败',
                'assignmentId.integer' => '施工项参数类型错误',
                'amount.required' => '获取施工量参数失败',
                'amount.numeric' => '施工量参数类型错误',
                'price.required' => '获取单价参数失败',
                'price.numeric' => '单价参数类型错误',
                'totalPrice.required' => '获取总价失败',
                'totalPrice.numeric' => '总价类型错误',
            ];
            $assignment = $param['data'];
            $batch = $workmanAssignmentModel->getBatch([
                'projectId' => $param['projectId'],
                'areaId' => $param['areaId'],
                'sectionId' => $param['sectionId'],
                'professionId' => $param['professionId'],
                'groupId' => $param['groupId'],
            ]);
            $batch++;
            foreach ($assignment as $a) {
                $validator1 = Validator::make($a, $rules1, $message1);
                $tmp = [
                    'projectId' => $param['projectId'],
                    'groupId' => $param['groupId'],
                    'areaId' => $param['areaId'],
                    'sectionId' => $param['sectionId'],
                    'professionId' => $param['professionId'],
                    'assignmentId' => $a['assignmentId'],
                    'amount' => $a['amount'],
                    'price' => $a['price'],
                    'totalPrice' => $a['totalPrice'],
                    'adminId' => $adminId,
                    'remark' => $a['remark'],
                ];
                if ($validator1->passes()) {
                    if ($param['save'] == 1) {
                        if (empty($a['id'])) {
                            $tmp['batch'] = $batch;
                            $tmp['completeTime'] = date('Y-m-d H:i:s');
                            $workmanAssignmentModel->insert($tmp);
                        } else {
                            $info = $workmanAssignmentModel->info(['id' => $a['id']]);
                            if ($info['batch'] == 0) {
                                $tmp['batch'] = $batch;
                            }
                            $workmanAssignmentModel->update($tmp, ['id' => $a['id']]);
                        }
                    } else {
                        if (empty($a['id'])) {
                            $tmp['completeTime'] = date('Y-m-d H:i:s');
                            $workmanAssignmentModel->insert($tmp);
                        } else {
                            $workmanAssignmentModel->update($tmp, ['id' => $a['id']]);
                        }
                    }
                } else {
                    throw new ApiException($validator1->errors()->first(), 322402);
                }
            }
        } else {
            throw new ApiException($validator->errors()->first(), 322401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addAccount(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'areaId' => 'required|integer',
            'sectionId' => 'required|integer',
            'groupId' => 'required|integer',
            'data' => 'required|array'
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'areaId.required' => '获取施工段参数失败',
            'areaId.integer' => '施工段参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'groupId.required' => '获取班组参数失败',
            'groupId.integer' => '班组参数类型错误',
            'data.required' => '获取分账数据失败',
            'data.array' => '分账参数类型错误',
        ];
        $param = $request->only(['projectId', 'areaId', 'sectionId', 'groupId', 'data']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rules1 = [
//                'memberId' => 'required|integer',
                'employeeId' => 'required|integer',
                'account' => 'required|numeric',
            ];
            $message1 = [
//                'memberId.required' => '获取班组成员参数失败',
//                'memberId.integer' => '班组成员参数类型错误',
                'employeeId.required' => '获取工人参数失败',
                'employeeId.integer' => '工人参数类型错误',
                'account.required' => '获取分账金额参数失败',
                'account.numeric' => '分账金额参数类型错误',
            ];
            $accounts = $param['data'];
            $adminId = $request->session()->get('adminId');
            $workmanAccountsModel = new WorkmanAccountsModel();
            foreach ($accounts as $key => $a) {
                $validator1 = Validator::make($a, $rules1, $message1);
                if ($validator1->passes()) {
                    $tmp = [
                        'projectId' => $param['projectId'],
                        'areaId' => $param['areaId'],
                        'sectionId' => $param['sectionId'],
                        'groupId' => $param['groupId'],
//                        'memberId' => $a['memberId'],
                        'employeeId' => $a['employeeId'],
                        'account' => $a['account'],
                        'remark' => $a['remark'],
                        'separateTime' => date('Y-m-d H:i:s'),
                        'status' => 0,
                        'batch' => $a['batch'],
                        'adminId' => $adminId,
                    ];
                    if (empty($a['id'])) {
                        $tmp['createTime'] = date('Y-m-d H:i:s');
                        $workmanAccountsModel->insert($tmp);
                    } else {
                        $workmanAccountsModel->update($tmp, ['id' => $a['id']]);
                    }
                } else {
                    throw new ApiException($validator1->errors()->first(), 322502);
                }
            }
        } else {
            throw new ApiException($validator->errors()->first(), 322501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function handymanAccountsLists(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'nullable|integer',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:1',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'otherProjectId' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
//            'sectionId.required' => '获取楼层参数失败',
            'sectionId.integer' => '楼层参数类型错误',
//            'length.required' => '获取项目参数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
//            'page.required' => '获取起始记录参数失败',
            'page.integer' => '起始记录参数类型错误',
            'page.min' => '起始记录参数值不小于:min',
            'startTime.date_format' => '起始日期格式不正确',
            'endTime.date_format' => '截止日期格式不正确',
            'otherProjectId.integer' => '明细记录的项目参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'length', 'page', 'search', 'startTime', 'endTime', 'otherProjectId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['startTime'] > $param['endTime']) {
                throw new ApiException('起始日期不能大于截止日期', 322602);
            }
            $handymanAccountsModel = new HandymanAccountsModel();
            if (!empty($param['search'])) {
                unset($param['page'], $param['length']);
                $param['projectId'] = $param['otherProjectId'];
                $projectLists = $handymanAccountsModel->getHandymanProjects($param);
            }

            $lists = $handymanAccountsModel->lists($param);
            $countLists = $handymanAccountsModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
            if (!empty($param['search'])) {
                $this->data['projects'] = $projectLists;
            }
        } else {
            throw new ApiException($validator->errors()->first(), 322601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function handymanAccountsLists2(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'month' => 'nullable|date_format:Y-m',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'month.date_format' => '月份参数格式不正确',
        ];
        $param = $request->only(['projectId', 'month']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
        	if (empty($param['month'])){
				$param['month'] = date('Y-m');
			}
            $handymanAccountsModel = new HandymanAccountsModel();
            $lists = $handymanAccountsModel->lists2($param);
            $this->data = $this->tableFormat($lists, 0);
        } else {
            throw new ApiException($validator->errors()->first(), 322701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addHandymanAccounts(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'employeeId' => 'required|integer',
            'separateTime' => 'required|date_format:Y-m-d',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'employeeId.required' => '请选择工人',
            'employeeId.integer' => '工人参数类型错误',
            'separateTime.required' => '请选择分账时间',
            'separateTime.date_format' => '分账时间格式不正确',
        ];
        $param = $request->only(['projectId', 'employeeId', 'separateTime', 'data']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['separateTime'] > date('Y-m-d')) {
                throw new ApiException('记账时间不能超过当前日期', 322801);
            }
            $handymanAccountsModel = new HandymanAccountsModel();
            $list = $handymanAccountsModel->lists(['separateTime' => $param['separateTime'], 'employeeId' => $param['employeeId']]);
            if (!empty($list)) {
                foreach ($list as $l) {
                    if ($l->projectId != $param['projectId']) {
                        $this->msg = '保存成功，但是该工人当日已在其他项目录入账目';
                    }
                }
            }

            $adminId = $request->session()->get('adminId');
            foreach ($param['data'] as $d) {
                $insertData = [
                    'projectId' => $param['projectId'],
                    'employeeId' => $param['employeeId'],
                    'areaId' => $d['areaId'],
                    'sectionId' => $d['sectionId'],
                    'professionId' => $d['professionId'],
                    'assignmentId' => $d['assignmentId'],
                    'assignmentDetail' => $d['assignmentDetail'],
                    'account' => $d['account'],
                    'separateTime' => $param['separateTime'],
                    'adminId' => $adminId,
                    'createTime' => date('Y-m-d H:i:s'),
                ];
                $handymanAccountsModel->insert($insertData);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 322801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

//    /**
//     * @param Request $request
//     * @return \Illuminate\Http\Response
//     * @throws ApiException
//     */
//    public function editHandymanAccounts(Request $request)
//    {
//        $rules = [
//            'id' => 'required|integer',
//            'account' => 'required|numeric',
//            'areaId' => 'required|integer',
//            'sectionId' => 'required|integer',
//            'professionId' => 'required|integer',
//            'assignmentId' => 'required|integer',
//            'separateTime' => 'required|date_format:Y-m-d',
//        ];
//        $message = [
//            'id.required' => '获取记录参数失败',
//            'id.integer' => '记录参数类型错误',
//            'account.required' => '请填写分账金额',
//            'account.numeric' => '分账金额类型错误',
//            'separateTime.required' => '请选择分账时间',
//            'separateTime.date_format' => '分账时间格式不正确',
//            'areaId.required' => '请选择施工区',
//            'areaId.integer' => '施工区参数类型错误',
//            'sectionId.required' => '请选择楼层/施工段',
//            'sectionId.integer' => '施工段参数类型错误',
//            'professionId.required' => '请选择工种',
//            'professionId.integer' => '工种参数类型错误',
//            'assignmentId.required' => '请选择施工项',
//            'assignmentId.integer' => '施工项参数类型错误',
//        ];
//        $param = $request->only(['id', 'account', 'areaId', 'sectionId', 'professionId', 'assignmentId', 'separateTime', 'assignmentDetail']);
//        $validator = Validator::make($param, $rules, $message);
//        if ($validator->passes()) {
//            $handymanAccountsModel = new HandymanAccountsModel();
//            $projectAreaModel = new ProjectAreaModel();
//            $projectSectionModel = new ProjectSectionModel();
//            $id = $param['id'];
//            unset($param['id']);
//            $handymanAccountsModel->update(['id' => $id], $param);
//            $sum_section = $handymanAccountsModel->getSum(['sectionId' => $param['sectionId']]);
//            $sum_area = $handymanAccountsModel->getSum(['areaId' => $param['areaId']]);
//            $projectSectionModel->update(['id'=>$param['sectionId']],['handymanAccounts'=>$sum_section]);
//            $projectAreaModel->update(['id'=>$param['areaId']],['handymanAccounts'=>$sum_area]);
//        } else {
//            throw new ApiException($validator->errors()->first(), 322901);
//        }
//        return $this->ajaxResult($this->code, $this->msg, $this->data);
//    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function handymanAccountsInfo(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取记录参数失败',
            'id.integer' => '记录参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $handymanAccountsModel = new HandymanAccountsModel();
            $this->data = $handymanAccountsModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 323001);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delHandymanAccount(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取杂工账参数失败',
            'id.integer' => '杂工账参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $handymanAccountsModel = new HandymanAccountsModel();
            $handymanAccountsModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 323101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delAssignment(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取施工量价记录参数失败',
            'id.integer' => '班组施工量价记录参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $info = $workmanAssignmentModel->info($param);
            if ($info['status'] == 1) {
                throw new ApiException('账目尚未审核不能删除', 323202);
            } elseif ($info['status'] == 2) {
                throw new ApiException('账目已完成审核不能删除', 323203);
            } else {
                $result = $workmanAssignmentModel->delete($param);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 323201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delete(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取项目参数失败',
            'id.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectModel = new ProjectModel();
            $projectAreaModel = new ProjectAreaModel();
            $info = $projectAreaModel->info(['projectId' => $param['id']]);
            if ($info) {
                throw new ApiException('该项目下已有施工区，不能删除看此项目', 323402);
            }
            $this->data = $projectModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 323401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}